package com.magic0610.MagicYaml.BukkitConfiguration.file;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import org.apache.commons.lang.Validate;
import com.magic0610.MagicYaml.BukkitConfiguration.Configuration;
import com.magic0610.MagicYaml.BukkitConfiguration.InvalidConfigurationException;
import com.magic0610.MagicYaml.BukkitConfiguration.MemoryConfiguration;

public abstract class FileConfiguration extends MemoryConfiguration {
    public FileConfiguration() {
    }

    public FileConfiguration(Configuration defaults) {
        super(defaults);
    }

    public void save(File file) throws IOException {
        Validate.notNull(file, "File cannot be null");
        Files.createParentDirs(file);
        String data = saveToString();
        Writer writer = new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8);
        try {
            writer.write(data);
        } finally {
            writer.close();
        }
    }

    public void save(String file) throws IOException {
        Validate.notNull(file, "File cannot be null");
        save(new File(file));
    }

    public void load(File file) throws FileNotFoundException, IOException, InvalidConfigurationException {
        Validate.notNull(file, "File cannot be null");
        FileInputStream stream = new FileInputStream(file);
        load(new InputStreamReader(stream, Charsets.UTF_8));
    }

    public void load(Reader reader) throws IOException, InvalidConfigurationException {
        BufferedReader input = (reader instanceof BufferedReader) ? (BufferedReader) reader
                : new BufferedReader(reader);
        StringBuilder builder = new StringBuilder();
        try {
            String line;
            while ((line = input.readLine()) != null) {
                builder.append(line);
                builder.append('\n');
            }
        } finally {
            input.close();
        }
        loadFromString(builder.toString());
    }

    public void load(String file) throws FileNotFoundException, IOException, InvalidConfigurationException {
        Validate.notNull(file, "File cannot be null");
        load(new File(file));
    }

    public FileConfigurationOptions options() {
        if (this.options == null)
            this.options = new FileConfigurationOptions(this);
        return (FileConfigurationOptions) this.options;
    }

    public abstract String saveToString();

    public abstract void loadFromString(String paramString) throws InvalidConfigurationException;

    protected abstract String buildHeader();
}